package io.wongxd.demo.page

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import io.wongxd.demo.R
import io.wongxd.solution.baseView.BaseComposeFragment
import io.wongxd.solution.compose.custom.tooltips.TooltipPopup

class FgtTooltip : BaseComposeFragment() {
    override fun lazyInit() {

    }

    @Composable
    override fun Render() {
        Surface(modifier = Modifier.fillMaxSize(), color = Color.Gray) {
            val horizontalPadding = 24.dp

            val descriptionLarge = "Hello it i: Tooltips text with long long long description text"
            val descriptionMedium = "Tooltips text with description"
            val descriptionShort = "Tooltips text"


            Column(Modifier.fillMaxSize()) {
                Column(
                    modifier = Modifier
                        .padding(top = 32.dp)
                        .padding(horizontal = 16.dp)
                        .clip(RoundedCornerShape(10.dp))
                        .background(Color.White),
                ) {
                    TooltipContent(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 24.dp)
                            .padding(horizontal = horizontalPadding),
                        name = "Title 1",
                        value = "value",
                        tooltipsText = descriptionShort,
                    )

                    TooltipContent(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 24.dp)
                            .padding(horizontal = horizontalPadding),
                        name = "Description Medium",
                        value = "value",
                        tooltipsText = descriptionMedium,
                    )

                    TooltipContent(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 16.dp, bottom = 24.dp)
                            .padding(horizontal = horizontalPadding),
                        name = "Description Large",
                        value = "Tooltips text value",
                        tooltipsText = descriptionLarge,
                    )
                }
                Spacer(modifier = Modifier.weight(1f))
                Column(
                    modifier = Modifier
                        .padding(top = 32.dp, bottom = 24.dp)
                        .padding(horizontal = 16.dp)
                        .clip(RoundedCornerShape(10.dp))
                        .background(Color.White),
                ) {
                    TooltipContent(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 24.dp)
                            .padding(horizontal = horizontalPadding),
                        name = "tooltip bottom 1",
                        value = "value",
                        tooltipsText = descriptionMedium,
                    )

                    TooltipContent(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 16.dp, bottom = 24.dp)
                            .padding(horizontal = horizontalPadding),
                        name = "tooltip bottom 2",
                        value = "value",
                        tooltipsText = descriptionShort,
                    )
                }
            }
        }

    }

    @Composable
    fun TooltipContent(
        modifier: Modifier,
        tooltipsText: String,
        name: String,
        value: String,
    ) {
        Row(
            modifier = modifier,
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Tooltip(
                modifier = Modifier
                    .weight(1f)
                    .padding(end = 4.dp),
                name = name,
                tooltip = tooltipsText,
            )

            Text(
                text = value,
                color = Color.Black,
                style = TextStyle(
                    fontSize = 20.sp,
                    lineHeight = 22.sp,
                    fontWeight = FontWeight.Bold,
                ),
            )
        }
    }

    @Composable
    private fun Tooltip(
        modifier: Modifier,
        name: String,
        tooltip: String,
        color: Color = Color.Gray.copy(alpha = .45f)
    ) {
        Row(
            modifier = modifier,
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Text(
                modifier = Modifier.weight(weight = 1f, fill = false),
                text = name,
                maxLines = 1,
                style = TextStyle(
                    fontSize = 18.sp,
                    lineHeight = 22.sp,
                    fontWeight = FontWeight.Medium,
                ),
                color = color,
                overflow = TextOverflow.Ellipsis,
            )
            TooltipPopup(
                modifier = Modifier
                    .padding(start = 8.dp),
                requesterView = { modifier ->
                    Icon(
                        modifier = modifier,
                        painter = painterResource(id = R.drawable.svg_solution_info),
                        contentDescription = "TooltipPopup",
                        tint = color,
                    )
                },
                tooltipContent = {
                    Text(
                        modifier = Modifier
                            .padding(horizontal = 12.dp)
                            .padding(vertical = 8.dp),
                        text = tooltip,
                        style = TextStyle(
                            fontSize = 16.sp,
                            lineHeight = 18.sp,
                            fontWeight = FontWeight.Medium,
                        ),
                        color = Color.White,
                    )
                }
            )
        }
    }
}